package com.service.admin.borrow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.admin.borrow.controller.rep.MemberBorrowDetailsRep;
import com.service.admin.borrow.controller.rep.MemberBorrowPageRep;
import com.service.admin.borrow.controller.req.MemberBorrowPageReq;
import com.service.admin.borrow.service.MemberBorrowService;
import com.service.admin.borrow.service.converter.MemberBorrowConverter;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.mapper.MemberBorrowMapper;
import com.service.dao.mapper.MemberMapper;
import com.service.dao.model.Member;
import com.service.dao.model.MemberBorrow;
import com.service.dao.model.SysBorrowConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MemberBorrowServiceImpl extends ServiceImpl<MemberBorrowMapper, MemberBorrow>
	implements MemberBorrowService {

	@Autowired
	private MemberBorrowMapper memberBorrowMapper;

	@Autowired
	private MemberMapper memberMapper;
	/**
	 * 分页查询用户借贷列表
	 *
	 * @param req
	 * @return
	 */
	@Override
	public CommonPageRep<MemberBorrowPageRep> listPageByParams(MemberBorrowPageReq req) {
		List<Integer> memberIds = memberMapper.listMemberIdsByParam(req.getMobile(), req.getUserName());
		LambdaQueryWrapper<MemberBorrow> queryWrapper = new LambdaQueryWrapper<>();
		if(CollectionUtils.isNotEmpty(memberIds)){
			queryWrapper.in(MemberBorrow::getMemberId,memberIds);
		}
		if(req.getStartDate() != null){
			queryWrapper.ge(MemberBorrow::getBorrowDate,req.getStartDate());
		}
		if(req.getEndDate() != null){
			queryWrapper.le(MemberBorrow::getBorrowDate,req.getEndDate());
		}
		Page<MemberBorrow> page = PageHelper.startPage(req.getPageNo(), req.getPageSize());
		LambdaQueryWrapper<MemberBorrow> queryListWrapper = new LambdaQueryWrapper<>();
		queryListWrapper.orderByDesc(MemberBorrow::getUpdateTime);
		List<MemberBorrow> list = memberBorrowMapper.selectList(queryWrapper);
		return CommonPageRep.buildResultRep(page, MemberBorrowConverter.converter(list));
	}

	@Override
	public MemberBorrowDetailsRep details(Integer id) {
		MemberBorrowDetailsRep rep = new MemberBorrowDetailsRep();
		MemberBorrow memberBorrow = memberBorrowMapper.selectById(id);
		if(ObjectUtils.isNotEmpty(memberBorrow)){
			BeanUtils.copyProperties(memberBorrow,rep);
			Member member = memberMapper.selectById(memberBorrow.getMemberId());
			if (ObjectUtils.isNotEmpty(member)) {
				rep.setMemberName(member.getUserName());
				rep.setMemberPhone(member.getMobile());
			}
		}
		return rep;
	}
}
